package com.hb.trade.server.project.section.service.raw.impl;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.project.section.mapper.BidDownRecordMapper;
import com.hb.core.pojo.project.section.entity.BidDownRecord;
import com.hb.trade.server.project.section.service.raw.BidDownRecordService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 供应商招标文件下载记录表 服务实现类
 * </p>
 *
 * @author zhongdl
 * @since 2021-12-31
 */
@Service
public class BidDownRecordServiceImpl extends ServiceImpl<BidDownRecordMapper, BidDownRecord> implements BidDownRecordService {
    /**
     *
     * @Description:
     * @Author: zhongdl
     * @Date:2022/1/4 15:24
     * @param sectionId:  标段id
     * @param bidderId: 投标人id
     * @param orgId: 企业id
     * @return: {@link java.lang.Boolean }
     **/
    @Override
    public Boolean downFlag(Long sectionId, Long bidderId, Long orgId) {
        QueryWrapper<BidDownRecord> queryWrapper = BidDownRecord.wrapper();
        queryWrapper
                .eq("section_id",sectionId)
                .eq("bidder_id",bidderId)
                .eq("org_id",orgId);
        return count(queryWrapper) > 0;
    }

    @Override
    public BidDownRecord getBidDownRecord(Long sectionId, Long bidderId, Long orgId) {
        QueryWrapper<BidDownRecord> queryWrapper = BidDownRecord.wrapper();
        queryWrapper
                .eq("section_id",sectionId)
                .eq("bidder_id",bidderId)
                .eq("org_id",orgId).last("limit 1");

        return getOne(queryWrapper);
    }

    /**
     * Description: 根据企业id和标段id查询招标文件下载列表
     * @author: fengbin
     * @date: 2022/2/28 16:25
     * @param orgId
     * @param sectionId
     @return: java.util.List<com.hb.core.pojo.project.section.entity.BidSection>
     */
    @Override
    public BidDownRecord findBidDownRecordInfoByOrgId(Long orgId, Long sectionId) {
        QueryWrapper<BidDownRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BidDownRecord::getSectionId,sectionId)
                .eq(BidDownRecord::getOrgId,orgId).last("limit 1");
        BidDownRecord bidDownRecord = baseMapper.selectOne(queryWrapper);
        return bidDownRecord;
    }

    @Override
    public Page<JSONObject> pageDownloadRecords(Page<JSONObject> pages, Long sectionId) {
        return baseMapper.pageDownloadRecords(pages, sectionId);
    }

    /**
     * 根据标段id查询下载记录
     *
     * @author tian
     * @date 2022-5-10 14:08
     * @param sectionId
     * @return List<BidDownRecordDTO>
     */
    @Override
    public List<JSONObject> listDownloadBySectionId(Long sectionId) {
        return baseMapper.listDownloadBySectionId(sectionId);
    }
}
